package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.pojo.dto.DepartmentPkAddDTO;
import com.xbongbong.pro.statistic.pojo.dto.DepartmentPkEditDTO;
import com.xbongbong.pro.statistic.pojo.dto.DepartmentPkListDTO;
import com.xbongbong.pro.statistic.pojo.dto.DepartmentPkSetSaveDTO;
import com.xbongbong.pro.statistic.pojo.dto.DepartmentPkSubSaveDTO;
import com.xbongbong.pro.statistic.pojo.vo.DepartmentPkAddVO;
import com.xbongbong.pro.statistic.pojo.vo.DepartmentPkEditVO;
import com.xbongbong.pro.statistic.pojo.vo.DepartmentPkListVO;
import com.xbongbong.pro.statistic.pojo.vo.DepartmentPkSetSaveVO;
import com.xbongbong.pro.statistic.pojo.vo.DepartmentPkSubSaveVO;
import com.xbongbong.pro.statistic.service.DepartmentPkService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 部门pk方案相关
 *
 * @author 徐俊杰
 * @version v1.0
 * @date 2019/3/21 13:50
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/chart/pk/department")
public class DepartmentPkController {
    @Resource
    private DepartmentPkService departmentPkService;

    /**
     * 获取部门绩效pk方案列表
     *
     * @param departmentPkListDTO 入参
     * @param br                  校验信息
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/3/21 14:38
     * @since v1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String departmentPkList(@RequestBody @Valid DepartmentPkListDTO departmentPkListDTO, BindingResult br) {
        XbbResponse<DepartmentPkListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentPkListVO departmentPkListVO = departmentPkService.departmentPkList(departmentPkListDTO);
                response = new XbbResponse<>(departmentPkListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 部门绩效pk方案新建
     *
     * @param departmentPkAddDTO 入参
     * @param br                 校验信息
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/3/22 20:49
     * @since v1.0
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String departmentPkAdd(@RequestBody @Valid DepartmentPkAddDTO departmentPkAddDTO, BindingResult br) {
        XbbResponse<DepartmentPkAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentPkAddVO departmentPkAddVO = departmentPkService.departmentPkAdd(departmentPkAddDTO);
                response = new XbbResponse<>(departmentPkAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 部门绩效pk方案编辑
     *
     * @param departmentPkEditDTO 入参
     * @param br                  校验信息
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/3/22 20:51
     * @since v1.0
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String departmentPkEdit(@RequestBody @Valid DepartmentPkEditDTO departmentPkEditDTO, BindingResult br) {
        XbbResponse<DepartmentPkEditVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentPkEditVO departmentPkEditVO = departmentPkService.departmentPkEdit(departmentPkEditDTO);
                response = new XbbResponse<>(departmentPkEditVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 部门pk方案-勾选是否包含下属部门
     *
     * @param departmentPkSubSaveDTO 入参
     * @param br                     校验信息
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/3/24 10:22
     * @since v1.0
     */
    @RequestMapping(value = "/sub/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String departmentPkSubSave(@RequestBody @Valid DepartmentPkSubSaveDTO departmentPkSubSaveDTO, BindingResult br) {
        XbbResponse<DepartmentPkSubSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentPkSubSaveVO departmentPkSubSaveVO = departmentPkService.departmentPkSubSave(departmentPkSubSaveDTO);
                response = new XbbResponse<>(departmentPkSubSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 部门pk方案设置：删除、排序
     *
     * @param departmentPkSetSaveDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/3/26 10:40
     * @since v1.0
     */
    @RequestMapping(value = "/set/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String departmentPkSetSave(@RequestBody @Valid DepartmentPkSetSaveDTO departmentPkSetSaveDTO, BindingResult br) {
        XbbResponse<DepartmentPkSetSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentPkSetSaveVO departmentPkSetSaveVO = departmentPkService.departmentPkSetSave(departmentPkSetSaveDTO);
                response = new XbbResponse<>(departmentPkSetSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
